package com.hangwei.paperless.adapter.accept;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.AcceptCheckInfo;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.widget.MyTextWatcher;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.ConformDialog;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;
import com.twelve.all.utils.Tools;

import java.util.List;

public class Item2Adapter extends BaseItemDraggableAdapter<AcceptCheckInfo.TBean, BaseViewHolder> {

    public static final String mEmptySelectText = "请选择 Please select";
    private boolean mEnable = true;

    public Item2Adapter(List<AcceptCheckInfo.TBean> data) {
        super(R.layout.item_page_view2, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final AcceptCheckInfo.TBean item) {
        helper.setIsRecyclable(false);
        helper.setText(R.id.tvSeq, Tools.checkedString(item.seq))
                .setText(R.id.etDes, Tools.checkedString(item.description))
                .setText(R.id.etRemark, Tools.checkedString(item.remark))
                .addOnClickListener(R.id.tvResultVal);
        EditText etDes = helper.getView(R.id.etDes);
        TextView tvResultVal = helper.getView(R.id.tvResultVal);
        if (TextUtils.isEmpty(item.resultVal)) {
            tvResultVal.setText(mEmptySelectText);
            tvResultVal.setTextColor(mContext.getResources().getColor(R.color.lightText));
        } else {
            tvResultVal.setText(item.resultVal);
            tvResultVal.setTextColor(mContext.getResources().getColor(R.color.black));
        }
        EditText etRemark = helper.getView(R.id.etRemark);
        if (mEnable) {
            etDes.setEnabled(item.isAdd);
            etDes.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.description = s.toString().trim();
                }
            });
            etRemark.addTextChangedListener(new MyTextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.remark = s.toString().trim();
                }
            });
        } else {
            etDes.setEnabled(false);
            etRemark.setEnabled(false);
        }
    }

    @Override
    public void onItemSwiped(final RecyclerView.ViewHolder viewHolder) {
        final int position = getViewHolderPosition(viewHolder);
        if (mData.get(position).isAdd)
            new ConformDialog(mContext, "删除这条数据？") {
                @Override
                protected void onConfirm() {
                    if (TextUtils.isEmpty(mData.get(position).id))
                        Item2Adapter.super.onItemSwiped(viewHolder);
                    else {//已经保存了
                        final LoadingDialog loadingDialog = new LoadingDialog(mContext);
                        loadingDialog.show();
                        String userId = SPUtil.getString(SPKey.Account.USER_ID);
                        StaticPresenter.removeReceiveRecordSheetRow(userId, mData.get(position).id, new CallBack<Object>() {
                            @Override
                            public void onCompleted(Object o) {
                                loadingDialog.dismiss();
                                Item2Adapter.super.onItemSwiped(viewHolder);
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onError() {
                                loadingDialog.dismiss();
                                ToastUtil.showCenterToast("删除失败");
                            }
                        });
                    }
                }

                @Override
                protected void onCancel() {
                    notifyDataSetChanged();
                }
            };
        else {
            ToastUtil.showCenterToast("原始数据不能删除！");
            notifyDataSetChanged();
        }

    }

    public void setEnable(boolean enable) {
        mEnable = enable;
    }
}
